/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/***************************************************************************
 * Name: uds_transport.h
 *
 * Purpose: implementation unix domain socket transport(dialer and listener) ops for cbus
 *
 * Developer:
 *   wen.gu , 2022-03-01
 *
 * TODO:
 *
 ***************************************************************************/

/******************************************************************************
 **    INCLUDES
 ******************************************************************************/
#ifndef __UDS_TRANSPORT_H__
#define __UDS_TRANSPORT_H__

#include <stdio.h>
#include <WinSock2.h>
#include <afunix.h>

#include <Windows.h>
#include <stdint.h>

#pragma comment(lib,"wsock32.lib")

#undef GPTR

#include "cbus/core/cbus_type.h"
#include "cbus/core/cbus_error.h"
#include "cbus/bus/cbus_transport.h"
/******************************************************************************
 **    MACROS
 ******************************************************************************/

#define MAX_CLIENT_CONNECT_LIMIT 128

/** is the size of socket receive cache buffer */
#define SOCK_RECV_SIZE 1500

/** how many second will be block when call select */
#define SELECT_TIME_OUT_SEC 1

#define UDS_TRANSPORT_URL_REFIX "ipc://"
#define UDS_TRANSPORT_URL_PREFIX_LEN 6
/******************************************************************************
 **    TYPE DEFINITIONS
 ******************************************************************************/
G_C_HEADER_START

#ifdef _WIN64 
typedef GS64 socket_t;
#else
typedef GS32 socket_t;
#endif
/******************************************************************************
 **    CLASSES/FUNCTIONS DEFINITIONS
 ******************************************************************************/

/**
 * @brief create and initialize a dialer instance
 * @param handle [out] a instance of dialer
 * @return GErrc, success: G_OK, else error code.
 * 
*/
GErrc uds_dialer_initialize(GPHD* handle);

/**
 * @brief destroy a dialer instance
 * @param handle [in] a instance of dialer
 * @return GErrc, success: G_OK, else error code.
 * 
 */
GErrc uds_dialer_finish(GPHD handle);

/**
 * @brief connect to a listener(service)
 * @param handle [in] a instance of dialer
 * @param url [in] the url adress of listener(service)
 * @return GErrc, success: G_OK, else error code.
 * 
 */
GErrc uds_dialer_connect(GPHD handle, const GSTR url);

/**
 * @brief distconnect from listener(service)
 * @param handle [in] a instance of dialer
 * @return GErrc, success: G_OK, else error code.
 * 
 */ 
GErrc uds_dialer_disconnect(GPHD handle);

/**
 * @brief send data to remote side
 * @param handle [in] a instance handle for data transport
 * @param buf [in] data buffer
 * @param len [in] data length
 * @return G_OK: success, else: failed @see GErrc 
 * 
 */
GErrc uds_dialer_send(GPHD handle, GBUF buf, GU32 len);

/**
 * @brief set a receiver handler as callback to receive data from remote side
 * @param handle [in] a instance handler for data transport
 * @param opaque [in] a oapque handle of user for Receiver handler
 * @param on_receive [in] a callback for receive data from remote side
 * @return success:G_OK, else: error code @see GErrc
 * 
 */
GErrc uds_dialer_set_receiver(GPHD handle, cbus_dialer_receive_handler on_receive, void* opaque);

////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
 * @brief create and initialize a listener instance
 * @param handle [out] the instance of listener(service)
 * @param opaque [in] a oapque handle of user set to listener for callback function
 * @param on_dialer_status [in] a callback to observe the status of dialer which connec/disconnect
 *                          to/from current listener
 * @return GErrc, success: G_OK, else error code.
 * 
 */
GErrc uds_listener_initialize(GPHD* handle, cbus_on_dialer_status_func on_dialer_satus, void* opaque);

/**
 * @brief destroy a listener instance 
 * @param handle [in] the instance handle of listener
 * @return GErrc, success: G_OK, else error code.
 *  
 */
GErrc uds_listener_finish(GPHD handle);

/**
 * @brief offer a listener(service) with url address 
 * @param handle [in] the instance handle of listener
 * @param url [in] the url address of a listener(service)
 * @return GErrc, success: G_OK, else error code.
 */
GErrc uds_listener_bind(GPHD handle, GSTR url);

/**
 * @brief stop a listner(service)
 * @param handle [in] the instance handle of listener
 * @return GErrc, success: G_OK, else error code.
 */
GErrc uds_listener_unbind(GPHD handle);

/**
 * @brief send data to remote side
 * @param handle [in] the instance handle of listener
 * @param buf [in] data buffer
 * @param len [in] data length
 * @return G_OK: success, else: failed @see GErrc 
 * 
 */
GErrc uds_listener_send(GPHD handle, cbus_dialer_id dialer_id, GBUF buf, GU32 len);


/**
 * @brief set a receiver handler as callback to receive data from remote side 
 * @param handle [in] the instance handle of listener   
 * @param dialer_id  [in] if set, indicate this receive handler is only for this dialer(or client), if CBUS_INVALID_DIALER_ID then for all dialer,
 * @param on_receive [in] a callback for receive data from remote side
 * @param opaque [in] a oapque handle of user for Receiver handler 
 * @return success:G_OK, else: error code @see GErrc
 * 
 * @note  if set receive handler for all, then set receive handler for special dialer id will be not called 
 */
GErrc uds_listener_set_receiver(GPHD handle, cbus_dialer_id dialer_id, cbus_listener_receive_handler on_receive, void* opaque);


G_C_HEADER_END



#endif /** !__UDS_TRANSPORT_H__ */

